<?php

namespace App\Http\Controllers;

use Maatwebsite\Excel\Excel;

class ExcelController extends Controller
{

    //导出
    public function export(Excel $excel)
    {
        $file_name = iconv('UTF-8', 'GBK', '成绩');
        $type = 'xls';//xls,csv...
        $data = [
            ['学号', '姓名', '成绩'],
            ['10001', 'AAAAA', '99'],
            ['10002', 'BBBBB', '92'],
            ['10003', 'CCCCC', '95'],
            ['10004', 'DDDDD', '89'],
            ['10005', 'EEEEE', '96'],
        ];
        $excel->create($file_name, function ($export) use ($data) {
            $export->sheet('score', function ($sheet) use ($data) {
                $sheet->rows($data);
            });
        })->store('xls')->export($type);
    }

    //导入
    public function import(Excel $excel){
        $filePath = 'storage/exports/'.iconv('UTF-8', 'GBK', '成绩').'.xls';
        $result = $excel->load($filePath, function($reader) {})->get();
        dd($result);
    }

}

